
// -----------------------------------------------------------------------------

#pragma once

#include "Win32.MemoryBuffer.h"

namespace Kif
{
namespace Win32
{

enum BaseType
{
   BaseType_Unknown    = 0,
   BaseType_Boolean    = 1,
   BaseType_Byte       = 2,
   BaseType_Int16      = 3,
   BaseType_Int32      = 4,
   BaseType_Int64      = 5,
   BaseType_AnsiChar   = 6,
   BaseType_WideChar   = 7,
   BaseType_AnsiString = 8,
   BaseType_WideString = 9,
   BaseType_Float      = 11,
   BaseType_Double     = 12,
   BaseType_Decimal    = 12,
   BaseType_Currency   = 12,
   BaseType_DateTime   = 13,
   BaseType_Blob       = 99
}  ;  // end of BaseType

/// <summary>define the Nullable type</summary>
template<typename T, BaseType BT>
class Nullable
{
private:
   Kif::Win32::MemoryBuffer<T> memBuffer;
   DWORD sizeInBytes;

   T *value;  // pointer to an instance of the type
   BaseType BT;      // store the type of instance

public:

   Nullable()
   {
      sizeInBytes = sizeof(T);
      memBuffer.Alloc(sizeInBytes);
      value = NULL;
   }

  ~Nullable()
   {
      memBuffer.Free();
   }

   inline bool IsNull()
   {
      return(value == NULL);
   }

   inline T* GetBuffer()
   {
      return(value);
   }

   T operator=(T value)
   {
      if (this->value == NULL)
         this->value = memBuffer.Buffer;
      *this->value = value;
      return(*this->value);
   }  ;

   T operator=(T* value)
   {
      if (this->value == NULL)
         this->value = memBuffer.Buffer;
      if (value == NULL)
      {
         this->value = NULL;
         return(value);
      }
      *this->value = *value;
      return(*this->value);
   }  ;

   bool operator==(T value)
   {
      if (this->value == NULL)
         return(false);
      return(value == *this->value);
   }  ;

}  ;

typedef Nullable<bool,BaseType_Boolean> Boolean;
typedef Nullable<short,BaseType_Int16> Int16;
typedef Nullable<long,BaseType_Int32> Int32;

}  // end of Kif.Win32
}  // end of Kif

